/*jslint node: true */
var Promise = require('bluebird');
var cheerio = require('cheerio');
var request = Promise.promisifyAll(require('request'));

/**
 * Fetch ISE news.
 */
exports.iseReport = function () {
	return request.getAsync('http://www.ise.neu.edu.cn/?cat=22').spread(function (resp, body) {
		var $ = cheerio.load(body);
		var $news = $('#container > div.container_right > div.news_billboard_box > div > ul > li');
		return $news.get().map(function (e) {
			var $e = $(e), $a = $e.find('a'), $d = $e.find('span');
			var title = $a.attr('title').split(/\s*报告会\s*—*–*\s*|\s*——\s*/);
			return {
				title: title[1],
				speaker: title[0],
				link: $a.attr('href'),
				date: $d.text()
			};
		});
	});
};

exports.scNews = function () {
	return request.getAsync('http://sc.neu.edu.cn/jsp/newsAction!queryNewsBySectionId?sectionId=10027&page=1&size=12&_=1437631090808').spread(function (resp, body) {
		var data = JSON.parse(body);
		return data.newsList.map(function (e) {
			return {
				title: e.newsTitle,
				spearker: e.newsTitle,
				link: 'http://sc.neu.edu.cn/jsp/newsContent.jsp?newsId=' + e.newsId,
				date: e.newsDate,
			};
		});
	});
}
